package com.xiaochong.tir.manager.util;

import com.alibaba.fastjson.JSON;
import com.xiaochong.tir.common.data.po.ConceptCategory;
import com.xiaochong.tir.common.data.po.CurrencyIndustryCategory;
import com.xiaochong.tir.common.data.po.TransactionPair;
import com.xiaochong.tir.common.data.vo.BasePeriodHistoryVo;
import com.xiaochong.tir.common.data.vo.ExchangeInfoVo;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.manager.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by admin on 2018/6/2.
 */
@Component
public class SetExchangeIdex implements ApplicationRunner {

    @Autowired
    private RedisServer redisServer;
    @Autowired
    private ExchangeInfoService exchangeInfoService;
    @Autowired
    private TransactionPairService transactionPairService;
    @Autowired
    private CurrencyInfoService currencyInfoService;
    @Autowired
    private CurrencyConceptCategoryService currencyConceptCategoryService;
    @Autowired
    private CurrencyIndustryMiddleService currencyIndustryMiddleService;
    @Autowired
    private ConceptCategoryService conceptCategoryService;
    @Autowired
    private CurrencyIndustryCategoryService industryService;


    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception {
        //交易所list缓存
        List<ExchangeInfoVo> list = exchangeInfoService.selectExchangeList(new ExchangeInfoVo());
        redisServer.set(RedisKeysUtils.BOURSE_NAME_INDEX, JSON.toJSONString(list));
        //拆分交易对
//        transactionPairService.selectByTraList(new TransactionPair());
        //基期列表初始化

        //权重id列表
        List<BasePeriodHistoryVo> history = currencyInfoService.selectByHistoryList();
        redisServer.set(RedisKeysUtils.WEIGHT_COEFFICIENT, JSON.toJSONString(history));

        //概念id列表
        ConceptCategory cc = new ConceptCategory();
        cc.setEnableFlag(true);
        cc.setDeleteFlag(false);
        List<ConceptCategory> conceptList =  conceptCategoryService.selectByEntityList(cc);
        if(null!= conceptList){
            for (int i = 0; i < conceptList.size(); i++) {
                ConceptCategory con = conceptList.get(i);
                List<BasePeriodHistoryVo> category = currencyConceptCategoryService.selectByHistoryList(con.getId());
                redisServer.set(RedisKeysUtils.CONCEPTION_HISTORY + "@" + con.getId(), JSON.toJSONString(category));
            }
        }

        //行业id列表
        CurrencyIndustryCategory cic =  new CurrencyIndustryCategory();
        cic.setEnableFlag(true);
        cic.setDeleteFlag(false);
        List<CurrencyIndustryCategory> industryList = industryService.selectByEntityList(cic);
        if(null!= industryList){
            for (int i = 0; i < industryList.size(); i++) {
                CurrencyIndustryCategory industry = industryList.get(i);
                List<BasePeriodHistoryVo> instudy = currencyIndustryMiddleService.selectByHistoryList(industry.getId());
                redisServer.set(RedisKeysUtils.INSTUDY_TRENDS + "@" + industry.getId(), JSON.toJSONString(instudy));
            }
        }

    }
}
